package com.example.culture.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.culture.common.ServerResponse;
import com.example.culture.domain.Classes;
import com.example.culture.domain.Group;
import com.example.culture.domain.User;
import com.example.culture.service.AdminService;
import com.example.culture.util.jwt.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 管理员数据管理
 */
@Controller
@CrossOrigin
@RequestMapping(value = "/admin")
public class AdminController {

    private static Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    public AdminService adminService;


    /**
     * 管理员添加班级分组
     * @param request
     * @param classes
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addClasses",method = RequestMethod.POST)
    public ServerResponse addClasses(HttpServletRequest request, @RequestBody Classes classes){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        return adminService.addClasses(classes);
    }

    /**
     * 修改班级信息
     * @param request
     * @param classes
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateClass",method = RequestMethod.POST)
    public ServerResponse updateClass(HttpServletRequest request,@RequestBody Classes classes){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        return adminService.updateClass(classes);
    }


    /**
     * 修改组别名称
     * @param request
     * @param group
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateGroup",method = RequestMethod.POST)
    public ServerResponse updateGroup(HttpServletRequest request, @RequestBody Group group){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        return adminService.updateGroup(group);
    }


    /**
     * 删除班级信息（包括分组）
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteClasses",method = RequestMethod.POST)
    public ServerResponse deleteClasses(HttpServletRequest request,@RequestBody  String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=JSON.parseObject(data);
        Integer classesId=jsonObject.getInteger("classesId");
        return adminService.deleteClasses(classesId);
    }

    /**
     * 删除分组信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteGroup",method = RequestMethod.POST)
    public ServerResponse deleteGroup(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject= JSON.parseObject(data);
        Integer groupId=jsonObject.getInteger("groupId");
        return adminService.deleteGroup(groupId);
    }



}
